//
//  ChineseString.m
//  YUChineseSorting
//
//  Created by yuzhx on 15/4/19.
//  Copyright (c) 2015年 BruceYu. All rights reserved.
//
#import "City.h"
#import "ChineseString.h"

@implementation ChineseString
//@synthesize string;
//@synthesize pinYin;



#pragma mark - 返回tableview右方 indexArray
+(NSMutableArray*)cityIndexArray:(NSArray*)cityArray

{
    

    NSMutableArray *tempArray = [self ReturnSortChineseCityArrar:cityArray];
    NSMutableArray *A_Result=[NSMutableArray array];
    NSString *tempString ;
 
    for (City* c in tempArray)
    {
        NSString *pinyin = [[c.firstLetter substringToIndex:1] uppercaseString];
        //不同
        if(![tempString isEqualToString:pinyin])
        {
            // NSLog(@"IndexArray----->%@",pinyin);
            [A_Result addObject:pinyin];
            tempString = pinyin;
        }
    }
    return A_Result;
}
#pragma mark - 返回tableview右方 indexArray
//+(NSMutableArray*)IndexArray:(NSArray*)stringArr
//
//{
//    NSMutableArray *tempArray = [self ReturnSortChineseArrar:stringArr];
//    NSMutableArray *A_Result=[NSMutableArray array];
//    NSString *tempString ;
//    
//    for (NSString* object in tempArray)
//    {
//        NSString *pinyin = [((ChineseString*)object).pinYin substringToIndex:1];
//        //不同
//        if(![tempString isEqualToString:pinyin])
//        {
//            // NSLog(@"IndexArray----->%@",pinyin);
//            [A_Result addObject:pinyin];
//            tempString = pinyin;
//        }
//    }
//    return A_Result;
//}

#pragma mark - 返回联系人
//+(NSMutableArray*)LetterSortArray:(NSArray*)stringArr
//{
//    NSMutableArray *tempArray = [self ReturnSortChineseArrar:stringArr];
//    NSMutableArray *LetterResult=[NSMutableArray array];
//    NSMutableArray *item = [NSMutableArray array];
//    NSString *tempString ;
//    //拼音分组
//    for (NSString* object in tempArray) {
//        
//        NSString *pinyin = [((ChineseString*)object).pinYin substringToIndex:1];
//        NSString *string = ((ChineseString*)object).string;
//        //不同
//        if(![tempString isEqualToString:pinyin])
//        {
//            //分组
//            item = [NSMutableArray array];
//            [item  addObject:string];
//            [LetterResult addObject:item];
//            //遍历
//            tempString = pinyin;
//        }else//相同
//        {
//            [item  addObject:string];
//        }
//    }
//    return LetterResult;
//}

#pragma mark - 返回联系人
+(NSMutableArray*)SortCityArray:(NSArray*)cityArray
{
  NSMutableArray *tempArray = [self ReturnSortChineseCityArrar:cityArray];
    NSMutableArray *LetterResult=[NSMutableArray array];
    NSMutableArray *item = [NSMutableArray array];
    NSString *tempString ;
    //拼音分组
    for (City* c in tempArray) {
     
        NSString *pinyin = [[c.firstLetter substringToIndex:1] uppercaseString];
//        NSString *string = c.name;
        //不同
        if(![tempString isEqualToString:pinyin])
        {
            //分组
            item = [NSMutableArray array];
            [item  addObject:c];
            [LetterResult addObject:item];
            //遍历
            tempString = pinyin;
        }else//相同
        {
            [item  addObject:c];
        }
    }
    return LetterResult;
}




//过滤指定字符串   里面的指定字符根据自己的需要添加
+(NSString*)RemoveSpecialCharacter: (NSString *)str {
    NSRange urgentRange = [str rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString: @",.？、 ~￥#&<>《》()[]{}【】^@/￡¤|§¨「」『』￠￢￣~@#&*（）——+|《》$_€"]];
    if (urgentRange.location != NSNotFound)
    {
        return [self RemoveSpecialCharacter:[str stringByReplacingCharactersInRange:urgentRange withString:@""]];
    }
    return str;
}





+(NSMutableArray*)ReturnSortChineseCityArrar:(NSArray*)cityArray
{
    //获取字符串中文字的拼音首字母并与字符串共同存放
    NSMutableArray *citySortArray=[NSMutableArray array];
    for(int i=0;i<[cityArray count];i++)
    {
        City * c=cityArray[i];
        if(c.name==nil){
            c.name=@"";
        }
        //去除两端空格和回车
        c.name = [c.name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        
        c.name=[ChineseString RemoveSpecialCharacter:c.name];
        
        NSString *regex = @"[A-Za-z]+";
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
        NSString *initialStr = [c.name length]?[c.name substringToIndex:1]:@"";
        if ([predicate evaluateWithObject:initialStr])
        {
            NSLog(@"chineseString.string== %@",c.name);
            //首字母大写
            c.name = [c.name capitalizedString] ;
        }

      
        [citySortArray addObject:c];
    }
    //按照拼音首字母对这些Strings进行排序
    NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"firstLetter" ascending:YES]];
    [citySortArray sortUsingDescriptors:sortDescriptors];
    

    
    
    return citySortArray;
    
}


///////////////////
//
//返回排序好的字符拼音
//
/////////////////////
//+(NSMutableArray*)ReturnSortChineseArrar:(NSArray*)stringArr
//{
//    //获取字符串中文字的拼音首字母并与字符串共同存放
//    NSMutableArray *chineseStringsArray=[NSMutableArray array];
//    for(int i=0;i<[stringArr count];i++)
//    {
//        ChineseString *chineseString=[[ChineseString alloc]init];
//        chineseString.string=[NSString stringWithString:[stringArr objectAtIndex:i]];
//        if(chineseString.string==nil){
//            chineseString.string=@"";
//        }
//        //去除两端空格和回车
//        chineseString.string  = [chineseString.string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//        
//        
//        //此方法存在一些问题 有些字符过滤不了
//        //NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@／：；（）¥「」＂、[]{}#%-*+=_\\|~＜＞$€^•'@#$%^&*()_+'\""];
//        //chineseString.string = [chineseString.string stringByTrimmingCharactersInSet:set];
//        
//        
//        //这里我自己写了一个递归过滤指定字符串   RemoveSpecialCharacter
//        chineseString.string =[ChineseString RemoveSpecialCharacter:chineseString.string];
//        // NSLog(@"string====%@",chineseString.string);
//        
//        
//        //判断首字符是否为字母
//        NSString *regex = @"[A-Za-z]+";
//        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
//        NSString *initialStr = [chineseString.string length]?[chineseString.string substringToIndex:1]:@"";
//        if ([predicate evaluateWithObject:initialStr])
//        {
//            NSLog(@"chineseString.string== %@",chineseString.string);
//            //首字母大写
//            chineseString.pinYin = [chineseString.string capitalizedString] ;
//        }else{
//            if(![chineseString.string isEqualToString:@""]){
//                NSString *pinYinResult=[NSString string];
//                for(int j=0;j<chineseString.string.length;j++){
//                    NSString *singlePinyinLetter=[[NSString stringWithFormat:@"%c",
//                                                   
//                                                   pinyinFirstLetter([chineseString.string characterAtIndex:j])]uppercaseString];
//                    //                    NSLog(@"singlePinyinLetter ==%@",singlePinyinLetter);
//                    
//                    pinYinResult=[pinYinResult stringByAppendingString:singlePinyinLetter];
//                }
//                chineseString.pinYin=pinYinResult;
//            }else{
//                chineseString.pinYin=@"";
//            }
//        }
//        [chineseStringsArray addObject:chineseString];
//    }
//    //按照拼音首字母对这些Strings进行排序
//    NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"pinYin" ascending:YES]];
//    [chineseStringsArray sortUsingDescriptors:sortDescriptors];
//    
//    for(int i=0;i<[chineseStringsArray count];i++){
//        //        NSLog(@"chineseStringsArray====%@",((ChineseString*)[chineseStringsArray objectAtIndex:i]).pinYin);
//    }
//    NSLog(@"-----------------------------");
//    
//    
//    return chineseStringsArray;
//    
//}

#pragma mark - 返回一组字母排序数组
//+(NSMutableArray*)SortArray:(NSArray*)stringArr
//{
//    NSMutableArray *tempArray = [self ReturnSortChineseArrar:stringArr];
//    
//    //把排序好的内容从ChineseString类中提取出来
//    NSMutableArray *result=[NSMutableArray array];
//    for(int i=0;i<[stringArr count];i++){
//        [result addObject:((ChineseString*)[tempArray objectAtIndex:i]).string];
//        NSLog(@"SortArray----->%@",((ChineseString*)[tempArray objectAtIndex:i]).string);
//    }
//    return result;
//}
@end